<?php
/*
 *      oslist.php
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscomponent.php");

class oslist extends oscomponent {
	
	protected $selected;
	//protected $value;
	protected $items = array();
	
	function __construct($parent) {
		parent::__construct($parent);
	}
	
	function additem($name, $value) {
		$this->beforeadditem($name);
		$this->items[$name] = array("value" => $value);
		if (!$this->selected) {
			$this->selected = $name;
		}
		$this->afteradditem($name);
	}
	
	function setproperty($name, $properties) {
		if (array_key_exists($name, $this->items)) {
			foreach ($properties as $key=>$value) {
				$this->items[$name][$key] = $value;
			}
		}
	}
	
	function getproperty($name, $property) {
		if (array_key_exists($name, $this->items)) {
			return $this->items[$name][$property];
		}
	}
	
	function deleteitem($name) {
		$this->beforedeleteitem($name);
		if (array_key_exists($name, $this->items)) {
			unset($this->items[$name]);
			if (count($this->items)>0) {
				$this->selected = key($this->items);
			} else {
				$this->selected = null;
			}
		}
		$this->afterdeleteitem($name);
	}
	
	function deleteallitems() {
		$this->items = array();
		$this->selectitem = null;
	}
	
	function selectitem($name) {
		$this->beforeselectitem($name);
		if (array_key_exists($name, $this->items)) {
			$this->selected = $name;
			$this->afterselectitem($name);
			return true;
		} else {
			$this->afterselectitem($this->selected);
			
			return false;
		}
		
	}
	
	function __get($name) {
		switch ($name) {
			case "selected":{return $this->selected;break;}
			case "value":{
				if ($this->selected) {
					if (array_key_exists($this->selected, $this->items)) {
						return $this->items[$this->selected]["value"];
					}
				}
				break;
			}
		}
	}
}

?>
